package com.google.code.netbuilder.dal.entities;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.SequenceGenerator;
import javax.xml.bind.annotation.*;


@XmlRootElement(name = "equipment")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "equipment")
@SequenceGenerator(sequenceName = "equipment_seq", name = "equipment_seq", initialValue = 1, allocationSize = 100)
public class Equipment extends ManagedObject {

	private static final long serialVersionUID = 1463552467877481585L;
	@XmlAttribute(name = "address")
    private String address;
    @XmlElement(name = "longitude")
    private double longitude;
    @XmlElement(name = "latityde")
    private double latityde;
    
    @XmlElementWrapper(name = "ports")
    @XmlElement(name = "port")
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(table = "port", name = "equipment_id")
    private List<Port> ports;

	private String type;
    
	public Equipment() {
        ports = new ArrayList<Port>();
        for (int i = 0; i < 10; i++) {
            Port p = new Port();
            p.setName(String.valueOf(i));
            ports.add(p);
        }
        for (int i = 0; i < 10; i += 2) {
            ports.get(i).setInnerConnectedPort(ports.get(i + 1));
            ports.get(i + 1).setInnerConnectedPort(ports.get(i));
        }
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public double getLatityde() {
        return latityde;
    }

    public void setLatityde(double latityde) {
        this.latityde = latityde;
    }
    
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

    public List<Port> getPorts() {
        return ports;
    }

    public void setPorts(List<Port> ports) {
        this.ports = ports;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    
    @Override
    public boolean equals(Object ob)
    {
        if(ob == null)
        {
            return false;
        }
        if(!(ob instanceof Equipment))
        {
            return false;
        }
        Equipment castedOb = (Equipment) ob;
        return super.equals(ob) && 
               (address != null ? address.equals(castedOb.getAddress()) : castedOb.getAddress() == null) &&
               (ports != null ? castedOb.getPorts().equals(ports) : castedOb.getPorts() == null);
    }

    @Override
    public String toString() {
        return "Equipment{" + super.toString() + ", address=" + address + ", type: " + type + ", ports=" + ports  + '}';
    }
    
    public int getNumOfFreePorts()
    {
        if(ports == null) {
            
            return 0;
        }
        
        int counter = 0;
        
        for(ListIterator<Port> i = ports.listIterator(); i.hasNext(); ) {
            
            if(i.next().isFree()) {
                
                counter++;
            }
                
        }
        
        return counter;
        
    }
}
